<template>
  <section class="link-box">
    <div class="link">
      <a href="javascript:void(0)"
         :title="value"
         @click="handleClick">{{ value }}
      </a>
    </div>
    <span v-if="row && row.combineContractNo"
          class="ml5 order-type">
      合单
    </span>
    <el-tooltip v-if="value"
                content="复制编号"
                placement="right"
                effect="light">
      <i class="el-icon-copy-document icon"
         @click="copy(value)" />
    </el-tooltip>
    <quote-matching-dialog v-if="visible"
                           :visible.sync="visible"
                           :preset="preset" />
  </section>
</template>

<script>
import { quotation, transport } from "@/api/index";
import { transformData } from '@/libs/util';
import QuoteMatchingDialog from "@/components/dialog/tableFrame/QuoteMatchingDialog.vue"
export default {
  name: "ValidateQuote",
  inject: ['$table'],
  components: { QuoteMatchingDialog },
  props: {
    row: {
      type: Object,
      default: () => { }
    },
    value: {
      type: String
    },
    // 合约匹配 1匹配 默认不匹配
    isMatch: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      visible: false,
      preset: {},
      transform: {
        quotationId: "quotationId",
        combineContractNo: "combineContractNo",
        pick: "pick",
        offLoad: "offLoad",
        waybillId: "waybillId",
        deliver: "deliver",
        onLoad: "onLoad",
        insure: "insure",
        combineId: "combineId",
        subjectId: "subjectId",
        status: "status"
      }
    }
  },
  computed: {
    tips () {
      return {
        3: "运单有关单待办需要处理，暂不能操作",
        5: "运单已对账，不能操作",
        6: "运单已结账，不能操作",
        7: "财务已将运单加入到对账单，不能操作，请联系财务处理"
      }
    }
  },
  methods: {
    //报价前置效验
    async quoteCheckValidate () {
      let response = await quotation.validateCheckQuotation(this.row.subjectId);
      if (response.code === 0) {
        if (response.data.validate) {
          // 待报价、草稿、被拒绝、补充报价需要调用validate接口再次校验
          if ([10, 0, 2, 3].includes(this.row.status)) {
            this.quoteValidate();
          } else {
            this.goDetail(this.row.status);
          }
        } else if ([3, 7].includes(response.data?.flag)) {
          return this.$message({ type: 'warning', message: this.tips[response.data.flag] })
        } else if ([5, 6].includes(response.data.flag)) {
          this.$confirm(
            this.tips[response.data?.flag],
            "提示",
            {
              confirmButtonText: "我知道了",
              type: "warning",
              showClose: false,
              showCancelButton: false,
              closeOnClickModal: false
            }
          ).then(() => {
            this.closeQuotaion(response.data?.flag);
          });
        }
      } else {
        this.$message({ type: "error", message: response.message });
      }
    },
    async quoteValidate () {
      let { data } = await quotation.validateQuote(this.row.subjectId);
      // data.validate true: 对方没有报价 false：对方已报价
      if (data.validate) {
        if (this.isMatch) {
          this.handleTableScheduler()
        } else {
          this.goDetail(this.row.status);
        }
      } else if (data.flag === 1) {
        this.$confirm("已向您发起了报价，是否去审核?", "提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.goDetail("11", data.quotationId);
        });
      } else if (data.flag === 2) {
        this.$confirm("不能重复报价，请刷新页面?", "提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.onload();
        });
      }
    },
    // 关闭报价单
    async closeQuotaion (closeType) {
      await transport.closeWaybillQuote({ waybillId: this.row.waybillId });
      // 刷列表 
      this.$table.search();
    },
    async handleTableScheduler () {
      let { data } = await quotation.isQuoteMatch({ subjectId: this.row.subjectId });
      if (data?.match) {
        // url: this.transform?.isRefresh ? `/quotation/refresh` : `/quotation/match`,
        this.preset = {
          param: transformData({ ...this.row }, this.transform),
          childName: {
            ...data           //合约匹配到的数据
          },
          url: `/quotation/match`,
        }
        this.visible = true;
      } else {
        this.goDetail(this.row.status);
      }
    },
    onload () {
      // 刷列表 
      this.$table.search();
    },
    handleClick () {
      if (this.row?.combineContractNo) {
        this.$router.push({
          name: "oneBillQuote",
          query: {
            combineId: this.row.combineId,
            subjectId: this.row.subjectId,
            surcharge: false,
            status: 10
          }
        });
      } else {
        this.quoteCheckValidate();
      }
    },
    // 前往详情页
    goDetail (status, quotationId) {
      let query = {
        quotationId: quotationId || this.row.quotationId,
        waybillId: this.row.waybillId,
        subjectId: this.row.subjectId,
        surcharge: this.row?.surcharge,
        status: status
      };
      this.$router.push({
        name: "quoteDetail",
        query
      });
    },
    copy (val) {
      // 模拟 输入框
      var cInput = document.createElement("input");
      cInput.value = val;
      document.body.appendChild(cInput);
      cInput.select(); // 选取文本框内容 // 执行浏览器复制命令 // 复制命令会将当前选中的内容复制到剪切板中（这里就是创建的input标签） // Input要在正常的编辑状态下原生复制方法才会生效
      document.execCommand("copy");
      this.$message({ type: "success", message: "复制成功" });
      // 复制成功后再将构造的标签 移除
      document.body.removeChild(cInput);
    }
  }
};
</script>
<style scoped>
.link-box {
  display: flex;
  align-items: center;
}
.link {
  max-width: 160px !important;
  color: var(--prev-color-primary) !important;
  display: block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  text-align: center;
}
.icon {
  margin-left: 2px;
  margin-top: 3px;
  width: 16px;
  height: 16px;
  cursor: pointer;
  color: chocolate;
}
</style>
